package ua.model;

import java.util.LinkedList;
import java.util.List;

import ua.common.XmlManager;

public abstract class UserListBean
{	
	private static TomcatUsersBean getTomcatUser()
	{	
		TomcatUsersBean tomcatUsersBean = (TomcatUsersBean) XmlManager.getInstance().getObjectXml();
		tomcatUsersBean.updateRolesFromStr();
		return tomcatUsersBean;
	}
	
	public static List<UserBean> getUsers(String key)
	{	
		List<UserBean> listeUsers = filter(getTomcatUser().getUsers(), key == null ? "" : key);
		return listeUsers;	
	}
	
	private static List<UserBean> filter(List<UserBean> listUser, String key) {
		List<UserBean> newList = new LinkedList<UserBean>();
		for (UserBean user : listUser) {
			if (user.getUsername().startsWith(key)) {
				newList.add(user);
			}
		}
		return newList;
	}
	
	public static UserBean getUserByUsername(String userName)
	{
		for (UserBean user : getTomcatUser().getUsers()) {
			if (user.getUsername().equals(userName)) {
				return user;
			}
	    }
		return null;  
	}
	public static UserBean getUserByName(String username)
	{
		return getTomcatUser().getUserByName(username);
	}
}
